﻿using System;
using System.Collections.Generic;

using System.Text;
using System.IO;
using System.Net;

namespace MSL.Utility.Net
{
    /// <summary>
    /// IP实体
    /// </summary>
    public class IpDetail
    {
        #region 属性
        /// <summary></summary>
        public string Ret { get; set; }
        /// <summary>开始</summary>
        public string Start { get; set; }
        /// <summary>结束</summary>
        public string End { get; set; }
        /// <summary>国家</summary>
        public string Country { get; set; }
        /// <summary>省</summary>
        public string Province { get; set; }
        /// <summary>县</summary>
        public string City { get; set; }
        /// <summary>区</summary>
        public string District { get; set; }
        /// <summary></summary>
        public string Isp { get; set; }
        /// <summary></summary>
        public string Type { get; set; }
        /// <summary>详细</summary>
        public string Desc { get; set; }

        #endregion
    }
    /// <summary>
    /// IP
    /// </summary>
    public class IpHelper
    {
        #region 获取IP地址的详细信息

        /// <summary>
        /// 获取IP地址的详细信息，调用的借口为
        /// http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={ip}
        /// 返回json，格式为如下 {"ret":1,"start":"59.174.77.0","end":"59.174.80.156","country":"\u4e2d\u56fd","province":"\u6e56\u5317","city":"\u6b66\u6c49","district":"","isp":"\u7535\u4fe1","type":"","desc":""}
        /// </summary>
        /// <param name="ipAddress">请求分析得IP地址</param>
        /// <param name="sourceEncoding">服务器返回的编码类型</param>
        /// <returns>IpUtils.IpDetail</returns>
        public static string IpDetail(String ipAddress, Encoding sourceEncoding)
        {
            string InpueUrl = String.Format("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={0}", ipAddress);
            WebRequest mywebreq = WebRequest.Create(InpueUrl);
            WebResponse mywebresp = mywebreq.GetResponse();
            StringBuilder htmltext = new StringBuilder();
            htmltext.Append("[");
            try
            {
                using (StreamReader sr = new StreamReader(mywebresp.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312")))
                {
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        htmltext.Append(line);
                    }
                    sr.Close();
                }
            }
            catch
            {
                //page.Response.Write("<Script>alert('读取文件错误')</Script>");
            }
            htmltext.Append("]");
            return htmltext.ToString();
        }
        #endregion

        #region 获得用户IP
        /// <summary>
        /// 获得用户IP
        /// </summary>
        public static string GetIp()
        {
            string ip;
            string[] temp;
            bool isErr = false;
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null)
                ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            else
                ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            if (ip.Length > 15)
                isErr = true;
            else
            {
                temp = ip.Split('.');
                if (temp.Length == 4)
                {
                    for (int i = 0; i < temp.Length; i++)
                    {
                        if (temp[i].Length > 3) isErr = true;
                    }
                }
                else
                    isErr = true;
            }

            if (isErr)
                return "1.1.1.1";
            else
                return ip;
        }
        #endregion
    }
}
